package com.imooc.course.business.controller.admin;

import com.imooc.course.business.form.CourseForm;
import com.imooc.course.business.form.CoursePageForm;
import com.imooc.course.server.dto.*;
import com.imooc.course.server.entity.CourseCategory;
import com.imooc.course.server.service.CourseService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/course")
public class CourseController {

    private static final Logger log = LoggerFactory.getLogger(CourseController.class);
    public static final String BUSINESS_NAME = "课程";


    @Resource(name = "courseService")
    private CourseService courseService;

    /**
     * 查询列表
     * @param pageForm 传入的page和size
     * @return
     */
    @PostMapping("/list")
    public ResponseDto getList(@RequestBody CoursePageForm pageForm) {
        CoursePageDto pageDto = CopyUtil.copy(pageForm, CoursePageDto.class);
        courseService.getList(pageDto);
        return ResponseDto.success().setContent(pageDto);
    }

    /**
     * 查询该课程所属分类
     * @param courseId
     * @return
     */
    @PostMapping("/list-category/{courseId}")
    public ResponseDto getCategoryList(@PathVariable String courseId) {
        List<CourseCategory> categoryList = courseService.getCategoryList(courseId);
        return ResponseDto.success().setContent(categoryList);
    }

    /**
     * 查找文本内容
     * @param id
     * @return
     */
    @GetMapping("/find-content/{id}")
    public ResponseDto findContent(@PathVariable String id) {
        CourseContentDto ccd = courseService.findById(id);
        return ResponseDto.success().setContent(ccd);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param courseForm
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addCourse(@RequestBody CourseForm courseForm) {
        //入参检查
        ValidatorUtil.notNull(courseForm.getName(), "名称");
        ValidatorUtil.minimumToMaximum(courseForm.getName(), "名称", 1, 50);
        ValidatorUtil.minimumToMaximum(courseForm.getSummary(), "概述", 1, 2000);
        ValidatorUtil.minimumToMaximum(courseForm.getImage(), "封面", 1, 100);
        ValidatorUtil.notNull(courseForm.getCategorys(),"分类管理列表");

        CourseDto courseDto = CopyUtil.copy(courseForm, CourseDto.class);

        List<CategoryDto> categoryList = courseForm.getCategorys().stream()
                .map(category -> CopyUtil.copy(category, CategoryDto.class))
                .collect(Collectors.toList());
        courseDto.setCategoryDtoList(categoryList);

        courseService.save(courseDto);
        return ResponseDto.success().setContent(courseDto);
    }

    @PostMapping("/save-content")
    public ResponseDto saveContent(@RequestBody CourseContentDto courseContentDto) {
        courseService.saveContent(courseContentDto);
        return ResponseDto.success();
    }

    @PostMapping("/sort")
    public ResponseDto setTheSort(@RequestBody SortDto sortDto) {
        courseService.setTheSort(sortDto);
        return ResponseDto.success();
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        courseService.delete(id);
        return ResponseDto.success();
    }
}
